﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Data;
using System.Windows.Ria;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.ViewModels
{
    public sealed class ProjectCategoryEditorViewModel : IProjectCategoryEditorViewModel
    {
        #region IProjectMemberEditorViewModelConsume Members

        private PagedCollectionView sourceCategories;
        public PagedCollectionView SourceCategories
        {
            get { return this.sourceCategories; }
            private set
            {
                this.sourceCategories = value;
                this.sourceCategories.MoveCurrentToFirst();
                this.PropertyChanged.Notify(() => this.SourceCategories);
            }
        }

        private PagedCollectionView projectCategories;
        public PagedCollectionView ProjectCategories
        {
            get { return this.projectCategories; }
            private set
            {
                this.projectCategories = value;
                this.projectCategories.MoveCurrentToFirst();
                this.PropertyChanged.Notify(() => this.ProjectCategories);
            }
        }

        private Action<object> addToProject;
        public Action<object> AddToProject
        {
            get { return this.addToProject; }
            set
            {
                this.addToProject = obj =>
                {
                    value.TryInvoke(obj);
                    this.RefreshProjectCategories();
                };
            }
        }

        private Action<object> removeFromProject;
        public Action<object> RemoveFromProject
        {
            get { return this.removeFromProject; }
            set
            {
                this.removeFromProject = obj =>
                {
                    value.TryInvoke(obj);
                    this.RefreshProjectCategories();
                };
            }
        }

        public Action Completed { get; set; }

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region IProjectMemberEditorViewModelService Members

        private EntityCollection<ProjectCategory> categories;
        public EntityCollection<ProjectCategory> Categories
        {
            get { return this.categories; }
            set
            {
                this.categories = value;
                this.ProjectCategories = new PagedCollectionView(this.categories);
            }
        }

        private IEnumerable<CategorySummary> categorySource;
        public IEnumerable<CategorySummary> CategorySource
        {
            get { return this.categorySource; }
            set
            {
                this.categorySource = value;
                this.RefreshProjectCategories();
            }
        }

        #endregion

        #region Methods
        private void RefreshProjectCategories()
        {
            var filteredCategories = this.CategorySource.Where(category => this.Categories.Any(pc => pc.CategoryID == category.CategoryID) == false);
            this.SourceCategories = new PagedCollectionView(filteredCategories);
            this.SourceCategories.SortDescriptions.Add(new SortDescription(
                    ObjectReflection.GetPropertyName<CategorySummary>(c => c.Name),
                    ListSortDirection.Ascending));
        }
        #endregion Methods

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        ~ProjectCategoryEditorViewModel()
        {
            this.Dispose();
        }
    }
}
